package rpg.visual.mapEditor;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import pgui.PVisual;
import rpg.map.Creature;
import rpg.map.Item;
import rpg.map.Location;
import rpg.map.Map;
import rpg.map.MapObject;
import rpg.visual.VisualImage;
import rpg.visual.VisualMap;
import square.data.DataGroup;

/**
 * 
 * @author Pedro Silva Moreira
 */
public class VisualMapEditor extends VisualMap {
	// Map Editor Variables
	private Location workLocationHover;
	private Location workLocation;
	private String workName;
	private String workType;
	private String workAction = "select";
	private Integer currentZ = 7;

	// Creation
	public VisualMapEditor(PVisual visual) {
		super(visual);
	}

	// Event Reaction
	public Location location(MouseEvent event) {
		int x = (int) Math.floor(Math.abs(event.getX())
				/ VisualImage.SQUARE_SIZE);
		int y = (int) Math.floor(Math.abs(event.getY())
				/ VisualImage.SQUARE_SIZE);
		int z = getCurrentZ();

		if (getMap() == null)
			return null;

		Location location = getMap().getLocation(x, y, z);
		setClip(false);
		return location;
	}

	public MapObject workObject(Location location) {
		if (location.getCreature() != null) {
			return (MapObject) location.getCreature().getOwner();
		}

		if (location.getItems().size() > 0)
			return (MapObject) location.getItems()
					.get(location.getItems().size() - 1).getOwner();

		return null;
	}

	public void select(Location location) {
		if (!workAction.equals("select")) {
			return;
		}
	}

	public void insert(Location location) {
		if (!workAction.equals("insert")) {
			return;
		}

		if (workType.equals("creature")) {
			Creature insert = new Creature(getWorkName());

			if (insert != null) {
				location.setCreature(null);
				location.setCreature(insert);
			}
		} else if (workType.equals("item")) {
			Item insert = new Item(getWorkName());

			if (insert != null)
				location.addItem(insert);
		}
	}

	public void remove(Location location) {
		if (!workAction.equals("remove")) {
			return;
		}

		MapObject object = workObject(location);
		if (object != null)
			object.removeFromMap();
	}

	public void move(Location location) {
		if (!workAction.equals("select")) {
			return;
		}

		MapObject workObject = workObject(getWorkLocation());
		if (workObject == null)
			return;

		if (workLocation != location) {
			if (workObject instanceof Creature)
				location.setCreature((Creature) workObject);
			else if (workObject instanceof Item)
				location.addItem((Item) workObject);
		}
	}

	public void insertDragged(Location location) {
		if (!getWorkAction().equals("insert") || getWorkLocation() == location)
			return;
		insert(location);
	}

	public void removeDragged(Location location) {
		if (!getWorkAction().equals("remove") || getWorkLocation() == location)
			return;
		remove(location);
	}

	// Mouse Events
	@Override
	public void onMousePressed(MouseEvent event) {
		Location location = location(event);
		if (location == null)
			return;

		select(location);
		insert(location);
		remove(location);

		setWorkLocation(location);
	}

	@Override
	public void onMouseMoved(MouseEvent event, MouseEvent lastEvent) {
		Location location = location(event);
		if (location == null)
			return;

		setWorkLocationHover(location);
	}
	
	@Override
	public void onMouseDragged(MouseEvent event, MouseEvent lastEvent) {
		Location location = location(event);
		if (location == null)
			return;

		move(location);
		insertDragged(location);
		removeDragged(location);

		setWorkLocation(location);
		setWorkLocationHover(location);
	}
	
	// Start
	public void start() {
		this.setMap((Map) DataGroup.findMap("rook"));
	}

	// Draw Action
	@Override
	public void drawEffects(Graphics2D g, Location location, int cicle,
			double startTranslateX, double startTranslateY) {
		super.drawEffects(g, location, cicle, startTranslateX, startTranslateY);

		if (workLocation == location || workLocationHover == location) {
			g.translate(startTranslateX - g.getTransform().getTranslateX(),
					startTranslateY - g.getTransform().getTranslateY());
			g.translate(location.getX() * VisualImage.SQUARE_SIZE,
					location.getY() * VisualImage.SQUARE_SIZE);

			if (workLocation == location) {
				g.setColor(new Color(255, 0, 0));
			} else {
				g.setColor(new Color(0, 255, 0));
			}
			g.drawRect(0, 0, VisualImage.SQUARE_SIZE, VisualImage.SQUARE_SIZE);
		}
	}
	
	@Override
	public void onPaint(Graphics2D g) {
		setWidth((int) this.getVisual().getWidth());
		setHeight((int) this.getVisual().getHeight());
		super.onPaint(g);
	}

	// Get & Set
	public Location getWorkLocation() {
		return workLocation;
	}

	public void setWorkLocation(Location workLocation) {
		this.workLocation = workLocation;
	}

	public Location getWorkLocationHover() {
		return workLocationHover;
	}

	public void setWorkLocationHover(Location workLocationHover) {
		this.workLocationHover = workLocationHover;
	}

	public String getWorkName() {
		return workName;
	}

	public void setWorkName(String workName) {
		this.workName = workName;
	}

	public String getWorkType() {
		return workType;
	}

	public void setWorkType(String workType) {
		this.workType = workType;
	}

	public String getWorkAction() {
		return workAction;
	}

	public void setWorkAction(String workAction) {
		this.workAction = workAction;
	}

	public Integer getCurrentZ() {
		return currentZ;
	}

	public void setCurrentZ(Integer currentZ) {
		this.currentZ = currentZ;
	}
}
